package org.linlinjava.litemall.admin.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallGroupBuyDetailMapper;
import org.linlinjava.litemall.db.dao.LitemallGroupBuyMapper;
import org.linlinjava.litemall.db.dao.LitemallGroupUserMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhy
 * @date 2019-07-08 17:32
 **/
@Service
public class AdminGroupBuyService {
	@Resource
	private LitemallGroupBuyMapper litemallGroupBuyMapper;
	@Resource
	private LitemallGroupBuyDetailMapper litemallGroupBuyDetailMapper;
	@Resource
	private LitemallGroupUserMapper litemallGroupUserMapper;

	public int findNum(LocalDateTime start,LocalDateTime end) {
		return litemallGroupBuyMapper.findNum(start,end);
	}

	public LitemallGroupBuy activeGroup(){
		LitemallGroupBuyExample example = new LitemallGroupBuyExample();
		example.or().andStartTimeLessThanOrEqualTo(LocalDateTime.now()).andEndTimeLessThanOrEqualTo(LocalDateTime.now()).andRunningFlagEqualTo((short)1);
	    return litemallGroupBuyMapper.selectOneByExample(example);
	}

	public int updateGroupBuy(LitemallGroupBuy litemallGroupBuy) {
		return litemallGroupBuyMapper.updateByPrimaryKeySelective(litemallGroupBuy);
	}

	public int insertGroup(LitemallGroupBuy litemallGroupBuy){
		return litemallGroupBuyMapper.insertSelective(litemallGroupBuy);
	}

	public int insertGroupDetail(LitemallGroupBuyDetail litemallGroupBuyDetail){
		return litemallGroupBuyDetailMapper.insertSelective(litemallGroupBuyDetail);
	}

	public int updateGroup(LitemallGroupBuy litemallGroupBuy){
		return litemallGroupBuyMapper.updateByPrimaryKeySelective(litemallGroupBuy);
	}

	public int deleteGroup(Integer id){
		return litemallGroupBuyMapper.logicalDeleteByPrimaryKey(id);
	}

	public List<LitemallGroupBuy> getGroupList(String name,Integer page, Integer size){
		LitemallGroupBuyExample example = new LitemallGroupBuyExample();
		LitemallGroupBuyExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(name)){
			criteria.andRemarkLike(name);
		}
		criteria.andDeletedEqualTo(false);
		criteria.example().orderBy(LitemallGroupBuy.Column.endTime.desc());
		PageHelper.startPage(page, size);
        return litemallGroupBuyMapper.selectByExample(example);
	}

	public long getDetail(Integer groupId) {
		LitemallGroupBuyExample example = new LitemallGroupBuyExample();
		example.or().andRunningFlagEqualTo((short)1).andIdEqualTo(groupId);
		return litemallGroupBuyMapper.countByExample(example);
	}
	public int getDetailNumById(Integer id){
		LitemallGroupBuyExample example = new LitemallGroupBuyExample();
		example.or().andStartTimeLessThan(LocalDateTime.now()).andIdEqualTo(id);
		return (int)litemallGroupBuyMapper.countByExample(example);
	}

	public List<LitemallGroupBuyDetail> getDetailList(Integer id,Integer page, Integer size){
		LitemallGroupBuyDetailExample example = new LitemallGroupBuyDetailExample();
		example.or().andGroupIdEqualTo(id);
		PageHelper.startPage(page, size);
		return litemallGroupBuyDetailMapper.selectByExample(example);
	}

	public int updateGroupBuyDetail(Integer id,Integer num){
		LitemallGroupBuyDetail litemallGroupBuyDetail = new LitemallGroupBuyDetail();
		litemallGroupBuyDetail.setId(id);
		litemallGroupBuyDetail.setLeastNum(num);
		litemallGroupBuyDetail.setUpdateTime(LocalDateTime.now());
		return litemallGroupBuyDetailMapper.updateByPrimaryKeySelective(litemallGroupBuyDetail);
	}

	public int deleteGroupDetail(Integer id){
		return litemallGroupBuyDetailMapper.logicalDeleteByPrimaryKey(id);
	}

	public int deleteGroupDetailByGroupId(Integer id){
		return litemallGroupBuyDetailMapper.logicalDeleteByGroupId(id);
	}

	public List<LitemallGroupUser> getGroupUserList(Integer detailId,Integer page, Integer size){
		LitemallGroupUserExample example = new LitemallGroupUserExample();
		example.or().andIdEqualTo(detailId);
		PageHelper.startPage(page, size);
		return litemallGroupUserMapper.selectByExample(example);
	}

}
